﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ViberBS
{
    public partial class Form1 : Form
    {
        int xd, yd,xn,yn, xc, yc, xt, yt, xs, ys;
        [DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll")]
        public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        private const int MOUSEEVENTF_RIGHTUP = 0x10;


        

        public Form1(int xdp, int ydp,int xnp,int ynp,int xcp,int ycp,int xtp,int ytp,int xsp,int ysp)
        {
            xd = xdp;
            yd = ydp;
            xc = xcp;
            yc = ycp;
            xt = xtp;
            yt = ytp;
            xs = xsp;
            ys = ysp;
            xn = xnp;
            yn = ynp;
            InitializeComponent();
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }
        public void DoMouseClick(int x,int y)
        {
            
            //Call the imported function with the cursor's current position
            this.Cursor = new Cursor(Cursor.Current.Handle);
            Cursor.Position = new Point(x, y);
            
            int xu = Cursor.Position.X;
            int yu = Cursor.Position.Y;
            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, (uint)xu, (uint)yu,0, 0);
        }
        public void DoMouseDClick(int x, int y)
        {

            //Call the imported function with the cursor's current position
            this.Cursor = new Cursor(Cursor.Current.Handle);
            Cursor.Position = new Point(x, y);

            int xu = Cursor.Position.X;
            int yu = Cursor.Position.Y;
            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, (uint)xu, (uint)yu, 0, 0);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            foreach (var line in richTextBox1.Lines)
            {
                SetForegroundWindow(GetExplorerProcess().MainWindowHandle);

                //Click On Dialer Button
                DoMouseClick(Convert.ToUInt16(xd), Convert.ToUInt16(yd));
                System.Threading.Thread.Sleep(1000);
                SetForegroundWindow(GetExplorerProcess().MainWindowHandle);
                //Double Click On Number And Clear It
                DoMouseClick(xn, yn);
                System.Threading.Thread.Sleep(1000);
                SendKeys.Send("^{a}");
                SetForegroundWindow(GetExplorerProcess().MainWindowHandle);
                System.Threading.Thread.Sleep(1000);

                SendKeys.SendWait("{BACKSPACE}");                DoMouseClick(xn, yn);


                SetForegroundWindow(GetExplorerProcess().MainWindowHandle);
                //Simluate type
                SendKeys.SendWait(line);
                System.Threading.Thread.Sleep(1000);
                SetForegroundWindow(GetExplorerProcess().MainWindowHandle);
                //Click On Compose
                DoMouseClick(Convert.ToUInt16(xc), Convert.ToUInt16(yc));
                System.Threading.Thread.Sleep(1000);
                SetForegroundWindow(GetExplorerProcess().MainWindowHandle);
                //Click On Type

                DoMouseClick(Convert.ToUInt16(xt), Convert.ToUInt16(yt));
                System.Threading.Thread.Sleep(1000);
                SetForegroundWindow(GetExplorerProcess().MainWindowHandle);
                //Simulate Type

                SendKeys.SendWait(richTextBox2.Text);
                System.Threading.Thread.Sleep(1000);
                SetForegroundWindow(GetExplorerProcess().MainWindowHandle);
                //Click On Send 

                DoMouseClick(Convert.ToUInt16(xs), Convert.ToUInt16(ys));
            }
        }
        public static Process GetExplorerProcess()
        {
            var all = Process.GetProcessesByName("Viber");
            Process process = null;
            foreach (var p in all)
                if (process == null || p.StartTime > process.StartTime)
                    process = p;
            return process;
        }
        
    }
}
